﻿/*
 * Authors:    Kęstutis Džiautas
 *             Edmundas Matusevičius
 * University: VU, MIF, SE, 4 course
 * Date:       2010-12-18
 * Version:    1.0
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SemAnalyzer
{
    public class VariableTable
    {
        public VariableTable()
        {
            VarTable = new List<VariableNode>();
        }

        public void NewChar(string name)
        {
            newNode = new VariableNode(name, 0, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }
        public void NewInteger(string name)
        {
            newNode = new VariableNode(name, 1, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }
        public void NewFloat(string name)
        {
            newNode = new VariableNode(name, 2, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }
        public void NewVoid(string name)
        {
            newNode = new VariableNode(name, 3, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }
        public void NewStruct(string name)
        {
            newNode = new VariableNode(name, 4, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }
        public void NewEnum(string name)
        {
            newNode = new VariableNode(name, 5, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }


        public void NewArrayChar(string name, int dimension)
        {
            newNode = new VariableNode(name, 0, dimension, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }
        public void NewArrayInteger(string name, int dimension)
        {
            newNode = new VariableNode(name, 1, dimension, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }
        public void NewArrayFloat(string name, int dimension)
        {
            newNode = new VariableNode(name, 2, dimension, SemanticAnalyzer.currentDomain);
            VarTable.Add(newNode);
        }

        public void SetToConst(string name, string domain)
        {
            foreach (VariableNode vn in VarTable)
            {
                if (vn.Name.Equals(name) && vn.Domain.Equals(domain))
                {
                    vn.ConstVariable = 1;
                    break;
                }
            }
        }

        public void SetInicialized(string name, string domain)
        {
            foreach (VariableNode vn in VarTable)
            {
                if (vn.Name.Equals(name) && vn.Domain.Equals(domain))
                {
                    vn.Inicialized = 1;
                    break;
                }
            }
        }

        public void CleanDomain(string domain)
        {
            List<VariableNode> copy = VarTable;
            foreach (VariableNode vn in copy)
            {
                if (vn.Domain.Equals(domain))
                {
                    VarTable.Remove(vn);
                }
            }
        }

        /// <summary>
        /// Array to store the variable nodes
        /// </summary>
        List<VariableNode> VarTable;
        /// <summary>
        /// New variable node
        /// </summary>
        VariableNode newNode;
    }
}
